<?php
/**
 * Created by PhpStorm.
 * User: leno
 * Date: 17-5-18
 * Time: 下午3:37
 */

namespace Encore\Admin;


trait Attributes
{
    protected $attributes;

    protected $viewData = [];

    public function setValue($value)
    {
        $this->value();
        if ($value instanceof \Closure) {
            $this->value = call_user_func($value, $this);
        } else {
            $this->value = $value;
        }
        return $this;
    }

    public function getValue()
    {
        return $this->value;
    }

    public function setViewData($data)
    {
        $this->viewData = array_merge($this->viewData,$data);
        return $this;
    }

    public function getViewData()
    {
        return $this->viewData;
    }

    public function getAttributes()
    {
        return $this->attributes;
    }

    public function setAttributes($attributes, $merge = false)
    {
        $this->attributes = $merge ? array_merge($this->attributes, $attributes) : $attributes;
        return $this;
    }

    public function getAttribute($name)
    {
        return isset($this->attributes[$name]) ? $this->attributes[$name] : '';
    }

    public function setAttribute($name, $value, $merge = false)
    {
        if ($merge === true) {
            if (isset($this->attributes[$name])) {
                $attribute = explode(' ', $this->attributes[$name]);
                if (is_string($value)) {
                    $value = explode(' ', trim($value));
                }
                $attribute = array_merge($attribute, $value);
                $attribute = array_unique($attribute);
                $attribute = implode(' ', $attribute);
                $this->attributes[$name] = $attribute;
            } else {
                $this->attributes[$name] = $value;
            }
        } else {
            $this->attributes[$name] = $value;
        }
        return $this;
    }

    public function setView($view)
    {
        $this->view = $view;
        return $this;
    }
}